package com.book.service.impl;

import com.book.entity.BookEntity;
import com.book.entity.CartEntity;
import com.book.exception.GlobalException;
import com.book.service.CartService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class CartServiceImpl implements CartService {

    @Resource
    private RedisTemplate redisTemplate;

    private ObjectMapper objectMapper = new ObjectMapper();

    private static final String KEY =  "cartList";


    @Override
    public CartEntity addCart(BookEntity bookEntity, Long userId) throws JsonProcessingException {

        if(bookEntity == null || userId == null){
            throw new GlobalException(500,"添加购物车失败！！");
        }

        //先获取当前购物车,如果为空，创建购物车
        CartEntity cart = (CartEntity) redisTemplate.opsForHash().get(KEY, userId);
        if(cart == null){
            cart = new CartEntity();
            cart.getBooks().put(bookEntity.getId(),bookEntity);
            cart.setTotalCount(bookEntity.getCount());
            cart.setTotalPrice(bookEntity.getTotalPrice());
            redisTemplate.opsForHash().put(KEY,userId,cart);
            return getCart(userId);
        }else{
            BookEntity book = cart.getBooks().get(bookEntity.getId());
            if(book == null){
                cart.getBooks().put(bookEntity.getId(),bookEntity);
                cart.setTotalCount(cart.getTotalCount() + bookEntity.getCount());
                cart.setTotalPrice(cart.getTotalPrice().add(bookEntity.getTotalPrice()));
                redisTemplate.opsForHash().put(KEY,userId,cart);
                return getCart(userId);
            }else{
                book.setCount(book.getCount() + bookEntity.getCount());
                book.setTotalPrice(book.getTotalPrice().add(bookEntity.getTotalPrice()));
                cart.getBooks().put(book.getId(),book);
                cart.setTotalCount(cart.getTotalCount() + bookEntity.getCount());
                cart.setTotalPrice(cart.getTotalPrice().add(bookEntity.getTotalPrice()));
                redisTemplate.opsForHash().put(KEY,userId,cart);
                return getCart(userId);
            }
        }


    }

    @Override
    public CartEntity updateCart(Long bookId, Integer updateCount, Long userId) {
        if(bookId == null || userId == null ){
            throw new GlobalException(500,"修改购物车失败！！");
        }

        CartEntity cart = (CartEntity) redisTemplate.opsForHash().get(KEY, userId);
        if(cart == null || cart.getBooks().size() == 0 || cart.getBooks().get(bookId) == null){
            throw new GlobalException(500,"修改购物车失败！！");
        }
        BookEntity bookEntity = cart.getBooks().get(bookId);
        Integer changeCount = updateCount - bookEntity.getCount();
        bookEntity.setCount(updateCount);
        BigDecimal changePrice = bookEntity.getTotalPrice();
        bookEntity.setTotalPrice(bookEntity.getPrice().multiply(new BigDecimal(bookEntity.getCount())) );
        changePrice = bookEntity.getTotalPrice().subtract(changePrice);
        cart.getBooks().put(bookId,bookEntity);
        cart.setTotalPrice(cart.getTotalPrice().add(changePrice));
        cart.setTotalCount(cart.getTotalCount() + changeCount);
        redisTemplate.opsForHash().put(KEY,userId,cart);
        return getCart(userId);
    }

    @Override
    public CartEntity getCart(Long userId) {
        if(userId == null){
            throw new GlobalException(500,"获取购物车失败！！");
        }
        return (CartEntity) redisTemplate.opsForHash().get(KEY,userId);
    }

    @Override
    public CartEntity deleteCart(Long userId,Long bookId) {
        if(bookId == null || userId == null){
            throw new GlobalException(500,"删除购物车失败！！");
        }
        CartEntity cart = (CartEntity) redisTemplate.opsForHash().get(KEY, userId);
        if(cart == null || cart.getBooks().size() == 0 || cart.getBooks().get(bookId) == null){
            throw new GlobalException(500,"删除购物车失败！！");
        }
        BookEntity book = cart.getBooks().remove(bookId);
        cart.setTotalCount(cart.getTotalCount() - book.getCount());
        cart.setTotalPrice(cart.getTotalPrice().subtract(book.getTotalPrice()));
        redisTemplate.opsForHash().put(KEY,userId,cart);
        return cart;

    }

    @Override
    public void clearCart(Long userId) {
        if(userId == null){
            throw new GlobalException(500,"删除购物车失败！！");
        }
        redisTemplate.opsForHash().put(KEY,userId,null);
    }
}
